/*
 *  Copyright (c) 2018 Oray Inc. All rights reserverd.
 *  No Part of this file may be reproduced, stored
 *  in a retrieval system, or transmitted, in any form, or by any means,
 *  electronic, mechanical, photocopying, recording, or otherwise,
 *  without the prior consent of Oray Inc.
 *
 *  Author: qianwei
 *  Create on:  18-3-8 下午2:15
 *  FileName:RemoteDesktopUtils.java
 *
 *
 */

package com.bnq.control.common.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.bnq.control.common.dialog.LoadingDialog;
import com.bnq.control.desktop.view.Mouse;
import com.oray.sunlogin.manager.DesktopPluginManager;
import com.oray.sunlogin.manager.bean.desktop.DesktopMove;
import com.oray.sunlogin.plugin.remotedesktop.RemoteDesktopJni;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by qianwei on 2018/3/8.
 */

public class RemoteDesktopUtils {

    public static final int DIALOG_TYPE_LOAD_CHANGE_MODE = 3;

    private static final int TIME_OUT = 30 * 1000;
    private static final int MIN_STEP = 10;

    private static final int MESSAGE_EVENT_STATUS = 1;              // 监测鼠标键盘活动事件
    private static final int CHECK_EVENT_STATUS_TIME = 5 * 1000;    // 监测鼠标键盘状态时长

    private OnTimerOutListener mOnTimerOutListener;
    private RemoteDesktopJni jni;
    private LoadingDialog changeModeDialog;


    public RemoteDesktopUtils(RemoteDesktopJni jni) {
        this.jni = jni;
    }

    public RemoteDesktopUtils(DesktopPluginManager manager) {
        this.jni = manager.getDesktopPlugin();
    }

    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_EVENT_STATUS:
                    if (null != jni) jni.setActive(false);
                    startCheckEventStatusLooper();
                    break;
            }
        }
    };

    private void startCheckEventStatusLooper() {
        if (null != mHandler)
            mHandler.sendEmptyMessageDelayed(MESSAGE_EVENT_STATUS, CHECK_EVENT_STATUS_TIME);
    }

    public interface OnTimerOutListener {
        void onTimerOut(int dialogType);
    }

    public static boolean isEmpty(HashMap<Integer, List<Integer>> hashMap) {
        try {
            if (null == hashMap || hashMap.size() == 0) {
                return true;
            } else {
                boolean empty = true;
                for (Map.Entry<Integer, List<Integer>> entry : hashMap.entrySet()) {
                    List<Integer> listDates = entry.getValue();
                    if (null != listDates && listDates.size() > 0) {
                        empty = false;
                    }
                }
                return empty;
            }
        } catch (Exception e) {
            return true;
        }
    }

    public void setOnTimerOutListener(OnTimerOutListener listener) {
        this.mOnTimerOutListener = listener;
    }

    public void changeDesktopMode(Context context) {
        changeModeDialog = new LoadingDialog(context);
        changeModeDialog.setTips("正在切换...");
        changeModeDialog.setTimeOut(TIME_OUT);
        changeModeDialog.setOnTimeoutListener(() -> {
            if (null != mOnTimerOutListener)
                mOnTimerOutListener.onTimerOut(DIALOG_TYPE_LOAD_CHANGE_MODE);
        });
        changeModeDialog.show();
    }

    /**
     * 关闭切换模式的加载框
     */
    public void closeChangeModeDialog() {
        if (null != changeModeDialog && changeModeDialog.isShowing()) {
            changeModeDialog.dismissLoadingDialog();
        }
    }

    public static DesktopMove getDesktopMove(int dx, int dy, int height, int transX, int transY, Context context) {
        DesktopMove desktopMove = new DesktopMove();
        boolean stopMoveDesktop = false;
        int mDx = MIN_STEP;
        int mDy = 0;
        int mTransX = transX + dx;
        int mTransY = transY + dy;
        if (transX > DisplayUtils.getScreenWidth(context) -
                DisplayUtils.dp2px(Mouse.VIEW_SIZE - 25, context)) {
            mDx = -MIN_STEP;
        } else if (transX < 0) {
            mTransX = transX;
            mTransY = transY;
        } else if (transY > DisplayUtils.getScreenHeight(context)
                - DisplayUtils.dp2px(Mouse.VIEW_SIZE, context) - height) {
            mDx = 0;
            mDy = -MIN_STEP;
            mTransX = transX;
            mTransY = transY;
        } else if (transY < 0) {
            mDx = 0;
            mDy = MIN_STEP;
            mTransX = transX;
            mTransY = transY;
        } else {
            stopMoveDesktop = true;
        }
        desktopMove.setStopMoveDesktop(stopMoveDesktop);
        desktopMove.setDx(mDx);
        desktopMove.setDy(mDy);
        desktopMove.setLocationX(mTransX);
        desktopMove.setLocationY(mTransY);
        return desktopMove;
    }
}
